package org.jmhome.blog.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.NullArgumentException;
import org.jmhome.blog.model.Role;
import org.jmhome.blog.model.User;
import org.jmhome.blog.security.UserPrincipal;
import org.jmhome.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired(required = false)
	private PasswordEncoder passwordEncoder;

	@Autowired(required = false)
	private SaltSource saltSource;

	/**
	 * @param user
	 */
	protected void encodePassword(User user) {

		String password = user.getPassword();
		if (password != null && passwordEncoder != null) {
			Object salt = null;

			if (saltSource != null) {
				salt = saltSource.getSalt((new UserPrincipal(user, null)));
			}

			password = passwordEncoder.encodePassword(password, salt);
			user.setPassword(password);
		}
	}

	@RequestMapping(value = "/loginForm.do", method = RequestMethod.GET)
	public ModelAndView loginForm(ModelAndView mav) {
		mav.setViewName("login");
		return mav;
	}

	@RequestMapping(value = "/createUser.do", method = RequestMethod.POST)
	public ModelAndView createUser(ModelAndView mav,
			@ModelAttribute("User") User user) {
		if (user == null) {
			throw new NullArgumentException("user");
		}

		Role role = null;
		user.setRegDate(new Date());
		role = new Role();
		role.setRole("ROLE_USER");
		role.setBlogUser(user);
		user.setRole(role);
		encodePassword(user);
		userService.insertUser(user);

		mav.setViewName("redirect:/category.do");
		return mav;
	}

	@RequestMapping(value = "/updateUser.do", method = RequestMethod.POST)
	public ModelAndView updateUser(ModelAndView mav,
			@ModelAttribute("User") User updateUser, HttpSession session) {
		if (updateUser == null) {
			throw new NullArgumentException("user");
		}

		User user = (User) session.getAttribute("blogUser");
		if (user != null) {
			String userEmail = user.getEmail();
			String updateUserEmail = updateUser.getEmail();
			if (userEmail.equals(updateUserEmail)) {
				encodePassword(updateUser);

				String userPassword = user.getPassword();
				String updateUserPassword = updateUser.getPassword();
				if (userPassword.equals(updateUserPassword)) {
					userService.updateUser(updateUser);
				}
			}
		}
		mav.setViewName("redirect:/category.do");
		return mav;
	}

	@RequestMapping(value = "/logout.do", method = RequestMethod.GET)
	public ModelAndView logout(ModelAndView mav) {
		mav.setViewName("login");
		return mav;
	}

	// 컨트롤러 단위로 PropertyEditor를 등록함.
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));

	}
}
